import Mock from 'mockjs'
import { param2Obj } from '@/utils'

const List = []
const count = 100

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    name: '@csentence(3, 5)',
    img: undefined,
    'type|1': [{
      id: 1,
      name: '汽车'
    }, {
      id: 2,
      name: '书画'
    }, {
      id: 3,
      name: '古董'
    }],
    'hall|1': [{
      id: 1,
      name: 'A展馆'
    }, {
      id: 2,
      name: 'B展馆'
    }, {
      id: 3,
      name: 'C展馆'
    }],
    sponsor: '@csentence(3, 5)',
    contacts: '@cname',
    mobile: '@integer(10000)',
    contain: '@integer(100,20000)',
    area: '@integer(100,2000)',
    introduce: '@cparagraph(2, 8)',
    start_at: '@datetime("yyyy-MM-dd")',
    end_at: '@datetime("yyyy-MM-dd")',
    dateRange: '',
    place: ''
  }))
}

export default {
  getList: config => {
    const { importance, type, title, page = 1, limit = 20, sort } = param2Obj(config.url)

    let mockList = List.filter(item => {
      if (importance && item.importance !== +importance) return false
      if (type && item.type !== type) return false
      if (title && item.title.indexOf(title) < 0) return false
      return true
    })

    if (sort === '-id') {
      mockList = mockList.reverse()
    }

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

    return {
      total: mockList.length,
      items: pageList
    }
  },
  getTypes: config => {
    return {
      data: [{
        id: 1,
        name: '汽车'
      }, {
        id: 2,
        name: '书画'
      }, {
        id: 3,
        name: '古董'
      }]
    }
  },
  create: () => ({
    data: 'success'
  }),
  update: () => ({
    data: 'success'
  })
}
